**********************************************************************************************************************************************
* REPLICATION MAIN
**********************************************************************************************************************************************

**********************************************************************************************************************************************
* Settings
**********************************************************************************************************************************************

clear
use "BS_replication_data.dta", clear
xtset grid_id edate

**********************************************************************************************************************************************
* Programs 
**********************************************************************************************************************************************

{
**********************************************************************************************************************************************
capture program drop specb1
	program specb1
estadd scalar Ob = e(N)
estadd scalar Clust = e(N_clust)
estadd local gfe "$\checkmark$" , replace
estadd local yfe "" , replace	
estadd local cyfe "" , replace
estadd local mfe "" , replace
estadd local mefe "" , replace
estadd local Control "$\checkmark$", replace 
estadd local Conley "", replace 
	end
**********************************************************************************************************************************************
capture program drop specb2
	program specb2
	eststo clear
estadd scalar Ob = e(N)
estadd scalar Clust = e(N_clust)
estadd local gfe "$\checkmark$" , replace
estadd local yfe "$\checkmark$" , replace	
estadd local cyfe "" , replace
estadd local mfe "" , replace
estadd local mefe "" , replace
estadd local Control "$\checkmark$", replace 
estadd local Conley "", replace 
	end	
**********************************************************************************************************************************************		
capture program drop main
	program main
estadd scalar Ob = e(N)
estadd scalar Clust = e(N_clust)
estadd local gfe "$\checkmark$" , replace
estadd local yfe "$\checkmark$" , replace	
estadd local cyfe "" , replace
estadd local mfe "$\checkmark$" , replace
estadd local mefe "$\checkmark$" , replace
estadd local Control "$\checkmark$", replace 
estadd local Conley "", replace 
	end				
**********************************************************************************************************************************************
capture program drop ldmain
	program ldmain
estadd scalar Observations = e(N)
estadd local KG "$\checkmark$" , replace
estadd local CE "$\checkmark$" , replace	
	end	
**********************************************************************************************************************************************	
capture program drop ld2
	program ld2
estadd scalar Observations = e(N)
estadd local KG "$\checkmark$" , replace
estadd local CE "" , replace	
	end
**********************************************************************************************************************************************	
global scalar_2 "scalar("N N" "Clust Grid" "gfe Grid FE" "yfe Time FE" "mfe Month FE" "mefe Month*Equator FE" "cyfe Country*Year FE")"	
global scalar_3 "scalar("N N" "Clust Grid" "gfe Grid FE" "yfe Time FE" "mfe Month FE" "mefe Month*Equator FE")"	
global scalar_4 "scalar("N N" "Clust Grid" "gfe Grid FE" "yfe Time FE" "mfe Month FE" "mefe Month*Equator FE" "gyfe Grid*Year FE")"	
}

* First Execute where full data is needed

**********************************************************************************************************************************************
* 1. FIGURES
**********************************************************************************************************************************************
**********************************************************************************************************************************************
* FIGURE 1: The Dynamics of Temperature, Extreme Temperature Events and Conflicts
**********************************************************************************************************************************************

labmask edate, values(year)

preserve
collapse (mean) temp (firstnm) month, by(edate)
twoway (scatter temp edate, msymbol(o) mcolor(navy) mlabcolor(navy)) (fpfitci temp edate, lpattern(solid) clcolor(maroon)), ///
legend(off) ytitle("Mean Temperature") xtitle("") graphregion(color(white)) xlab(228(48)671, valuelabel)
graph export "figure1_a.pdf", replace
restore

preserve
collapse (mean) extreme (firstnm) month year, by(edate)
twoway (scatter extreme edate, msymbol(o) mcolor(navy) mlabcolor(navy)) (fpfitci extreme edate, lpattern(solid) clcolor(maroon)), ///
legend(off) ytitle("Mean Temperature Extremes High") xtitle("") graphregion(color(white)) xlab(228(48)671, valuelabel)
graph export "figure1_b.pdf", replace
restore

preserve
keep if year>=1997
collapse (mean) incidence (firstnm) month, by(edate)
twoway (scatter incidence edate, msymbol(o) mcolor(navy) mlabcolor(navy)) (fpfitci incidence edate, lpattern(solid) clcolor(maroon)), ///
legend(off) ytitle("Mean Incidence") xtitle("") graphregion(color(white)) xlab(444(48)671, valuelabel)
graph export "figure1_c.pdf", replace
restore

**********************************************************************************************************************************************
* Figure 4:Dynamics of Extreme Temperature Events in Africa
**********************************************************************************************************************************************

tsset grid_id edate
cap drop _spell _end _seq
tsspell extreme, c(extreme==1)

preserve
collapse (mean) extreme _seq, by(year)
twoway (scatter extreme year [w=_seq], msymbol(o) mcolor(navy) mlabcolor(navy)) (fpfit extreme year, lpattern(solid) clcolor(maroon) fintensity(inten10) ), ///
legend(off) ytitle("Mean Temperature Extreme High, weighted") xtitle("Year") graphregion(color(white)) xsize(8) 
graph export "figure4.pdf", replace
restore

* Now restrict on estimation sample 
**********************************************************************************************************************************************

keep if year>=1997

**********************************************************************************************************************************************
* FIGURE 3: Land Degradation and Population Dynamics
**********************************************************************************************************************************************

cap drop severitycat
gen severitycat = severity_ld
replace severitycat = round(severitycat,0.01)

cap drop migrationcat
gen migrationcat = migration_90_00
replace migrationcat = round(migrationcat,0.01)

preserve
collapse (mean) popgrowth00 (count) severity_ld, by(severitycat)
sum popgrowth, d
keep if popgrowth00<r(p95)
graph twoway (scatter popgrowth severitycat [w=severity_ld], msymbol(o) mcolor(navy) mlabcolor(navy)) ///
(lfit popgrowth severitycat, lpattern(solid)) ///
, graphregion(color(white)) ytitle("Population Growth Mean 1990-2000") xtitle("Severity Land Degradation 1990") ///
legend(off)
graph export "figure3_a.pdf", replace
restore

preserve
collapse (mean) migration_90_00 (count) severity_ld, by(severitycat)
sum migration_90_00, d
keep if migration_90_00<r(p95) 
graph twoway (scatter migration_90_00 severitycat [w=severity_ld], msymbol(o) mcolor(navy) mlabcolor(navy)) ///
(lfit migration_90_00 severitycat, lpattern(solid)) ///
, graphregion(color(white)) ytitle("Net Migration Mean 1990-2000") xtitle("Severity Land Degradation 1990") ///
legend(off)
graph export "figure3_b.pdf", replace
restore


**********************************************************************************************************************************************
* 2. TABLES
**********************************************************************************************************************************************
	
* \section{Short-Run}
**********************************************************************************************************************************************
* Table 1: Baseline Results: Extreme Temperature Events and Conflict
**********************************************************************************************************************************************

xtreg incidence extreme temp prec ///
, fe vce(robust)
specb1
est store e1

xtreg incidence extreme temp prec ///
i.year, fe vce(robust)
specb2
est store e2

xtreg incidence extreme temp prec ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store e3

*********************************************************************
esttab e1 e2 e3 using "table1.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* ) ar2 ///
$scalar_2 ///
nonotes noomitted noobs label ///
mtitles("" "" "" ""  "" "") se  ///
collabels("",lhs(Dep. var.: incidence civil conflict)) ///
transform(@*100 100)
*********************************************************************

**********************************************************************************************************************************************
* Table 2: Baseline Results: Extreme Temperature Events of Different Lengths
**********************************************************************************************************************************************

xtreg incidence extreme_1 temp prec ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store t1

xtreg incidence extreme_2 temp prec ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store t2

xtreg incidence extreme_p3 temp prec ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store t3

xtreg incidence extreme_1 extreme_2 extreme_p3 temp prec ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store t4

*********************************************************************		   		   
esttab t1 t2 t3 t4 using "table2.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") keep(extreme_1 extreme_2 extreme_p3) ar2  order(extreme_1 extreme_2 extreme_p3)  ///
$scalar_2 ///
nonotes noomitted noobs label ///
mtitles("" "" "" "") se  ///
collabels("",lhs(Dep. var.: incidence civil conflict)) ///
transform(@*100 100)
*********************************************************************

**********************************************************************************************************************************************
* Table 3: Baseline Results: Extreme Temperature and Conflict Types
**********************************************************************************************************************************************

*********************************************************************
global types "battle_terr battle_noterr agropastoral rural riot"
*********************************************************************

foreach x of global types {
xtreg `x' extreme temp prec i.month##i.above_equator ///
i.year, fe vce(robust) 
main 
est store `x'
}

*********************************************************************		   		   
esttab ${types} using "table3.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(temp prec _cons  *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* ) ar2 ///
$scalar_2 ///
nonotes noomitted noobs label ///
mtitles("Battle (terr)"  "Battle (non-terr)" "Agropastoral" "Rural Region" "Riot") se  ///
transform(@*100 100)
*********************************************************************	

**********************************************************************************************************************************************
* Table 4: Extreme Temperature Events and Conflict: The Role of Population and Productivity
**********************************************************************************************************************************************

xtreg incidence c.extreme##c.lnpopdens_ipo temp prec  ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store csie1
	   	   
xtreg incidence c.extreme##c.csi_mean temp prec  ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store csie2

xtreg incidence c.extreme##c.csi_mean c.extreme##c.lnpopdens_ipo temp prec  ///
i.year i.month##i.above_equator, fe vce(robust)
main
est store csie3

*********************************************************************		   		   
esttab csie1 csie2 csie3 using "table4.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  temp prec *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* ) ar2 ///
$scalar_2 ///
nonotes noomitted noobs label ///
mtitles("" "" "" "") se  ///
collabels("",lhs(Dep. var.: incidence civil conflict)) ///
transform(@*100 100)
*********************************************************************

**********************************************************************************************************************************************
* Table 5: Extreme Temperature Events and Conflict: The Role of Migration
**********************************************************************************************************************************************
*********************************************************************
* b) Population Density
*********************************************************************

preserve
keep if L12.popdens_ipo <= L12.popdens_25 & L12.popdens_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epq1
restore

preserve
keep if L12.popdens_ipo > L12.popdens_25 & L12.popdens_ipo <= L12.popdens_50 & popdens_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epq2
restore

preserve
keep if L12.popdens_ipo > L12.popdens_50 & L12.popdens_ipo <= L12.popdens_75 & popdens_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epq3
restore

preserve
keep if L12.popdens_ipo > L12.popdens_75  & popdens_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epq4
restore

*********************************************************************		   		   
esttab epq1 epq2 epq3 epq4 using "table5_a.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* temp prec) ar2 se  ///
$scalar_2 ///
nonotes noomitted noobs label mlabels(none) nolines nonumbers ///
transform(@*100 100)
*********************************************************************
*********************************************************************
* b) Population Growth
*********************************************************************

preserve
keep if L12.popgrowth_ipo <= L12.popgrowth_25 & L12.popgrowth_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epgq1
restore

preserve
keep if L12.popgrowth_ipo > L12.popgrowth_25 & L12.popgrowth_ipo <= L12.popgrowth_50 & popgrowth_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epgq2
restore

preserve
keep if L12.popgrowth_ipo > L12.popgrowth_50 & L12.popgrowth_ipo <= L12.popgrowth_75 & popgrowth_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epgq3
restore

preserve
keep if L12.popgrowth_ipo > L12.popgrowth_75  & popgrowth_ipo !=.
xtreg incidence extreme  temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store epgq4
restore

*********************************************************************		   		   
esttab epgq1 epgq2 epgq3 epgq4 using "table5_b.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* temp prec) ar2 se  ///
$scalar_2 ///
nonotes noomitted noobs label mlabels(none) nolines nonumbers ///
transform(@*100 100)
*********************************************************************
*********************************************************************
* c) Net Migration
*********************************************************************

preserve
sum migration_90_00, d 
keep if migration_90_00 < r(p25) & migration_90_00 !=.
xtreg incidence extreme temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store enmq1
restore

preserve
sum migration_90_00, d 
keep if migration_90_00 >= r(p25) & migration_90_00 < r(p50)  & migration_90_00 !=.
xtreg incidence extreme temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store enmq2
restore

preserve
sum migration_90_00, d 
keep if migration_90_00 >= r(p50) & migration_90_00 < r(p75) & migration_90_00 !=.
xtreg incidence extreme temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store enmq3
restore

preserve
sum migration_90_00, d 
keep if migration_90_00 >= r(p75) & migration_90_00 !=.
xtreg incidence extreme temp prec i.month##i.above_equator ///
i.year , fe vce(robust)
main
est store enmq4
restore

*********************************************************************		   		   
esttab enmq1 enmq2 enmq3 enmq4 using "table5_c.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* temp prec) ar2 se ///
$scalar_2 ///
nonotes noomitted noobs label mlabels(none) nolines nonumbers ///
transform(@*100 100)
*********************************************************************


**********************************************************************************************************************************************
* Table 6: Extreme Temperature Events and Conflict: The Role of Land Degradation
**********************************************************************************************************************************************

xtreg incidence c.extreme##c.severity_ld temp prec i.year i.month##i.above_equator ///
, fe vce(robust)
main
est store lde1

xtreg incidence extreme temp prec i.year i.month##i.above_equator ///
if severity_ld>=.7777778  , fe vce(robust)
main
est store lde2

xtreg incidence extreme temp prec i.year i.month##i.above_equator ///
if severity_ld<.7777778  , fe vce(robust)
main
est store lde3

*********************************************************************		   		   
esttab lde1 lde2 lde3 using "table6.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant interaction("$\times$") drop(_cons  temp prec *1* *2* 3* 4* 5* 6* 7* 8* 9* *eq* ) ar2 ///
$scalar_2 ///
nonotes noomitted noobs label ///
mtitles("All" "High Degradation" "Low Degradation") se  ///
collabels("",lhs(Dep. var.: incidence civil conflict)) ///
transform(@*100 100)
*********************************************************************   		   

* \section{Long-Run}
**********************************************************************************************************************************************
* Table 7: Extreme Weather Events and Conflict: Long-Differences 
**********************************************************************************************************************************************
**********************************************************************************************************************************************
* a: DiD 
**********************************************************************************************************************************************

preserve
collapse (mean) temp extreme incidence (firstnm) climate_id country_id, by(fh grid_id)
sort grid_id fh
cap drop diff_temp
bysort grid_id: gen diff_temp = temp[_n-1]-temp
cap drop diff_extreme
bysort grid_id: gen diff_extreme = extreme[_n-1]-extreme
cap drop diff_incidence
bysort grid_id: gen diff_incidence = incidence[_n-1]-incidence

keep if diff_temp!=.
label variable diff_temp "Diff Temp"
label variable diff_extreme "Diff Extreme"

* extremes
reg diff_incidence diff_extreme i.climate_id i.country_id, vce(robust)
ldmain
est store trend1
* temp
reg diff_incidence diff_temp i.climate_id i.country_id, vce(robust)
ldmain
est store trend2
* extremes and temp
reg diff_incidence diff_extreme diff_temp i.climate_id i.country_id, vce(robust)
ldmain
est store trend3

*********************************************************************
esttab trend1 trend2 trend3 using "table7_a.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant label interaction("$\times$") keep(diff_extreme diff_temp) ///
scalar(r2 "N N" "KG Climate Zone Trend" "CE Country Trend") ///
nonotes noomitted noobs ///
mtitles("" "" "" "" "" "") se  ///
collabels("",lhs(Dep. var.: diff civil conflict incidence))
*********************************************************************
restore

**********************************************************************************************************************************************
* b: GDD
**********************************************************************************************************************************************

preserve
cap drop period
gen period=.
replace period=1 if year==1997 | year==1998 | year==1999 | year==2000 | year==2001 | year==2002 
replace period=2 if year==2003 | year==2004 | year==2005 | year==2006 | year==2007 | year==2008
replace period=3 if year==2009 | year==2010 | year==2011 | year==2012 | year==2013 | year==2014 | year==2015 

collapse (mean) temp extreme incidence (firstnm) climate_id country_id, by(period grid_id)
sort grid_id period

cap drop diff_temp
bysort grid_id: gen dt = temp-temp[_n-1]
bysort grid_id: gen ddt = dt-dt[_n-1]

cap drop diff_extreme
bysort grid_id: gen de = extreme-extreme[_n-1]
bysort grid_id: gen dde = de-de[_n-1]

cap drop diff_incidence
bysort grid_id: gen dc = incidence-incidence[_n-1]
bysort grid_id: gen ddc = dc-dc[_n-1]

label variable dt "Diff Temp"
label variable de "Diff Extreme"

rename incidence c
rename temp t
rename extreme e

cap drop I23
gen I23 = 0
replace I23 = 1 if period==3

keep if dt!=.

*********************************************************************
* DiD with three sub-periods: Timing 

reghdfe dc de i.I23 , absorb(i.grid_id) vce(robust)
estadd local G "$\checkmark$" , replace	
est store trend21

reghdfe dc dt i.I23, absorb(i.grid_id) vce(robust)
estadd local G "$\checkmark$" , replace	
est store trend22

reghdfe dc de dt i.I23, absorb(i.grid_id) vce(robust)
estadd local G "$\checkmark$" , replace	
est store trend23


*********************************************************************
esttab trend21 trend22 trend23 using "table7_b.tex", replace ///
star(* 0.10 ** 0.05 *** 0.01) noconstant label interaction("$\times$")  ///
scalar(r2 "N N" "G Grid Trend") ///
nonotes noomitted noobs ///
mtitles("" "" "" "" "" "") se  ///
collabels("",lhs(Dep. var.: diff civil conflict incidence))
*********************************************************************
restore

